/**
 * Created by LaoXu on 2017/12/22.
 * @description:用户登录业务逻辑
 */
require(['config'], function () {
    require(['jquery', 'xutil', 'xVerify'], function ($, xutil, xVerify) {
        // 获取DOM元素
        //  ----  登录页面DOM元素
        var $loginMobile = $('#login-mobile');
        var $loginPassword = $('#login-password');
        var $loginBtn = $('#login-btn');

        var localStorage = window.localStorage;

        var loginPage = {
            init(){
                this.bindEvent();
            },
            bindEvent(){
                var that = this;
                $loginBtn.on('click', function () {
                    var mobile = $loginMobile.val();
                    var password = $loginPassword.val();

                    var checkMobile = that.checkMobile(mobile);
                    var checkPwd = that.checkPwd(password);

                    if (!checkMobile.isSuc) {
                        console.log(checkMobile);
                        alert(checkMobile.info);
                        return;
                    }

                    if (!checkPwd.isSuc) {
                        console.log(checkPwd);
                        alert(checkPwd.info);
                        return;
                    }
                    $.post(xutil.baseUrl + '/api/user/userLogin', {mobile, password}, function (data) {
                        console.log(data);
                        var checkServerCode = that.checkServerCode(data);
                        if (checkServerCode.isSuc) {
                            // 跳转页面并存储Token信息至本地
                            localStorage.setItem(xutil.token, data.token);
                        } else {
                            alert(checkServerCode.info);
                        }

                    });
                });
            },
            // 校验手机号码
            checkMobile(mobile){
                var isSuc = false,
                    info = "";
                if (xVerify.isStrEmpty(mobile)) {
                    info = "请输入用户名";
                } else if (!xVerify.checkPhone(mobile)) {
                    info = "手机号码格式不正确";
                } else {
                    info = "校验成功";
                    isSuc = true;
                }
                return {isSuc, info};
            },
            // 校验密码
            checkPwd(password){
                var isSuc = false,
                    info = "";
                if (xVerify.isStrEmpty(password)) {
                    info = "请输入密码";
                } else if (!xVerify.checkPwd(password)) {
                    info = "请输入6~16位字母或数字";
                } else {
                    info = "校验成功";
                    isSuc = true;
                }
                return {isSuc, info};
            },
            // 校验服务器端是否成功
            checkServerCode(data){
                var isSuc = false,
                    info = "",
                    code = data.code,
                    dataInfo = data.datas;
                if (code === 0) {
                    isSuc = true;
                    info = '登录成功';
                } else if (code === 1) {
                    info = '用户名或密码错误，请重新输入';
                } else if (code === 3) {
                    info = '请输入您的用户名或密码';
                } else if (code === 4) {
                    info = '用户信息未找到';
                } else {
                    info = dataInfo;
                }
                return {isSuc, info};
            }
        };

        /**
         {code: 3, datas: "请检查您传递的参数"}
         {code: 4, datas: "用户信息未找到"}
         {code: 1, datas: "用户名或密码错误"}
         {code: 0, datas: "用户登录成功"}
         */

        loginPage.init();

    });
});
